/**
 * 
 */
package br.com.formulavendas.businessusercase;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.formulavendas.domainmodel.Contato;


/**
 * @author Fernando
 * 
 */
public class ManterContato extends BaseBusinessUserCase
	implements br.com.formulavendas.businessusercase.baseinterface.IBasePersistence<Contato>,
			br.com.formulavendas.businessusercase.baseinterface.IBaseFind<Contato> {

	public ManterContato() {}
	
	private static final Logger log = LoggerFactory.getLogger(ManterContato.class);

	public static final String DDD = "ddd";
	public static final String NUMERO = "numero";
	public static final String TIPO = "tipo";

	// Listagens
	@Override
	public List<Contato> findAll() {
		log.debug("finding all Contato instances");
		try {
			String queryString = "from ContatoEndereco";
			Query queryObject = this._session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public Contato findById(java.lang.Integer id) {
		log.debug("getting Contato instance with id: " + id);
		try {
			Contato instance = (Contato) this._session.get("Contato", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List<Contato> findByProperty(String propertyName, Object value) {
	      log.debug("finding Contato instance with property: " + propertyName
	            + ", value: " + value);
	      try {
	         String queryString = "from Contato as model where model." 
	         						+ propertyName + "= ?";
	         Query queryObject = this._session.createQuery(queryString);
			 queryObject.setParameter(0, value);
			 return queryObject.list();
	      } catch (RuntimeException re) {
	         log.error("find by property name failed", re);
	         throw re;
	      }
		}
	
	public List<Contato> findByDdd(Object ddd) {
		return this.findByProperty(DDD, ddd);
	}

	public List<Contato> findByNumero(Object numero) {
		return findByProperty(NUMERO, numero);
	}

	public List<Contato> findByTipo(Object tipo) {
		return findByProperty(TIPO, tipo);
	}

	// CRUD
	@Override
	public void save(Contato transientInstance) {
		log.debug("saving Contato instance");
		Transaction tranx = this._session.beginTransaction();
		try {			
			this._session.save(transientInstance);
			tranx.commit();			
			log.debug("save successful");
		} catch (RuntimeException re) {
			tranx.rollback();
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Contato persistentInstance) {
		log.debug("deleting Contato instance");
		try {
			this._session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
}
